<?php
/**
 * 商户端---优惠券管理
 */

namespace app\store\controllers\coupon;

use App\common\models\Coupon;
use App\store\controllers\StoreBaseController;
use App\store\services\CouponServices;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;


class CouponController extends StoreBaseController
{

    private  $thisClass = 'Coupon';

    public function index()
    {
        $data = [];
        return successJson('ok', $data);
    }

    /**
     * 优惠券--首页
     * @return JsonResponse
     */
    public function couponHomePage()
    {
        $storeId = request()->input('s_id', 0);

        //获取数据
        $data = self::getStoreData($this->thisClass, 'couponHomePage');

        return successJson('ok', $data);
    }

    /**
     * 专用券列表
     * @return JsonResponse
     */
    public function specialCoupon(Request $request)
    {
        $storeId = request()->input('s_id', 8);
        $data = $request->all();
        $data['store_id'] = $storeId;
        $list = CouponServices::specialCoupon($data);
        return successJson('ok', $list);
    }

    /**
     * 可购买引流券列表
     * @return JsonResponse
     */
    public function canBuyITCouponList(Request $request)
    {
        $storeId = request()->input('s_id', 0);
        $data = $request->all();
        $data = CouponServices::canBuyITCouponList($data);
        return successJson('ok', $data);
    }

    /**
     * 购买引流券
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\InvalidRequestException
     */
    public function buyIncreaseTrafficCoupon(Request $request)
    {
        $storeId = request()->input('s_id', 0);

        $data = $request->all();
        $list = CouponServices::buyITCoupon($data);
        return successJson('ok', $list);
    }

    /**
     * 已有引流券总量
     */
    public function myITCouponTotal(Request $request)
    {
        $storeId = request()->input('s_id', 0);
        $data = $request->all();
        $data = CouponServices::myITCouponTotal($data);
        return successJson('ok', $data);
    }

    /**
     * 已有引流券列表
     */
    public function myITCouponList(Request $request)
    {
        $storeId = request()->input('s_id', 8);
        $data = $request->all();
        $data['store_id'] = $storeId;
        $data = CouponServices::myITCouponList($data);
        return successJson('ok', $data);
    }

    /**
     * 停止销售
     */
    public function  haltSales(Request $request)
    {
        $storeId = request()->input('s_id', 0);

        $data = $request->all();
        $data['shop_id'] = $storeId;

        $list = CouponServices::haltSales($data);
        return successJson('ok', $list);
    }

    /**
     * 专用券总量
     */
    public function couponTotal(Request $request)
    {
        $storeId = request()->input('s_id', 8);
        $data = $request->all();
        $data['store_id'] = $storeId;
        $data = CouponServices::couponTotal($data);
        return successJson('ok', $data);
    }

    /**
     * 专用券列表
     */
    public function couponLists(Request $request)
    {
        $storeId = request()->input('s_id', 8);
        $data = $request->all();
        $data['store_id'] = $storeId;
        $data = CouponServices::couponLists($data);
        return successJson('ok', $data);
    }

    /**
     * 优惠券已使用记录
     */
    public function usageRecord(Request $request)
    {
        $storeId = request()->input('s_id', 8);
        $data = $request->all();
        $data['store_id'] = $storeId;
        //获取数据
        $data = CouponServices::getUseLog($data);
        return successJson('ok', $data);
    }

    /**
     * 新建、编辑优惠券
     */
    public function couponModify(Request $request)
    {
        $storeId = request()->input('s_id', 8);
        $data = $request->all();
        $data['shop_id'] = $storeId;
        $ruleArr = Coupon::modify_dedicated_rules();
        //编辑时不能改金额数量
        if (!empty($data['id'])) {
            unset($ruleArr['remitted_money']);
            unset($ruleArr['stock']);
            unset($data['remitted_money']);
            unset($data['stock']);
        }
        $validator = Validator::make(
            $data,
            $ruleArr,
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::modifyCoupon($data);
        if (!$list) {
            return errorJson('操作失败！', []);
        }
        return successJson('ok', $data);
    }

    /**
     * 优惠券详情
     */
    public function couponDetails(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::update_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::getDedicatedCouponDetail($data);

        return successJson('ok', $list);
    }

    /**
     * 删除优惠券
     */
    public function couponDelete(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            Coupon::update_rules(),
            Coupon::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = CouponServices::updateStatus($data);

        return successJson('ok', $list);
    }


}

